package com.mbuswidget;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class MBusWidgetInitalConfiguration extends Activity {
	// This class is called when widget is first placed on the screen. It is responible for calling
	// the mbus service and notifying it that it exists. 
	
	private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
		}

		if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
			finish();
		}

		final Context context = MBusWidgetInitalConfiguration.this;
		
		AppWidgetManager
				.getInstance(context);
		
		// Start/Send Service a message that app has been added to screen
		Intent bindIntent = new Intent(this, MBusWidgetService.class);
		bindIntent.putExtra(MBusWidgetConstants.WIDGET_CREATED, mAppWidgetId);
		startService(bindIntent);	
		
		//Log.w("mbuswidget", "Notified service widget id: " + mAppWidgetId + " has been added.");
		
//		RemoteViews views = new RemoteViews(context.getPackageName(),
//				R.layout.mbuswidget);
//		appWidgetManager.updateAppWidget(mAppWidgetId, views);
		
		Intent resultValue = new Intent();
		resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
		setResult(RESULT_OK, resultValue);
		finish();
	}

	public MBusWidgetInitalConfiguration() {
		super();
	}
}
